function [ svr_out ] = smspyview(scanstruct,opts,svr)
% function [ svr ] = smspyview([scanstruct],[opts])
% Plot a special-measure 2D sweep with spyview.
% scanstruct should be:
%   1) a filename
%   2) A structure with scanstruct.scan and scanstruct.data
%   3) empty (a file dialog will open)
% opts is an (optional) structure with fields:

%% process arguments
  if ~exist('scanstruct','var') || isempty('scanstruct')
    [scanstruct path]=uigetfile('sm*.mat');     
    if ~strcmp(path, [pwd '\']) 
       scanstruct=[path scanstruct];
    end
  end
  
  if ischar(scanstruct)
    fname=scanstruct;
    scanstruct=load(scanstruct);
  else
    fname='In-Memory';
  end
  
  if ~exist('opts','var')
      opts=struct();
  end
  
  opts=def(opts,'channel',1);
  
  if ~isfield(scanstruct,'data')
      error('No data!');
  end
  d=scanstruct.data{opts.channel};
  
  %% Recognize type of scan and process accordingly.
  % Handle 2D data
  if (ndims(d) == 2) && (length(scanstruct.scan.loops) == 2)
      x=scanstruct.scan.loops(1).rng;
      y=scanstruct.scan.loops(2).rng;
      xl=tostr(scanstruct.scan.loops(1).setchan);
      yl=tostr(scanstruct.scan.loops(2).setchan);
      if ~isempty(scanstruct.scan.loops(1).getchan)
          zl=tostr(scanstruct.scan.loops(1).getchan);
      else
          zl=tostr(scanstruct.scan.loops(2).getchan);
      end          
  % Handle pulsed data from spin qubits
  elseif (ndims(d)==2) && (length(scanstruct.scan.loops)==1) && isfield(scanstruct.scan,'data') && isfield(scanstruct.scan.data,'pulsegroups')
      x=plsinfo('xval',scanstruct.scan.data.pulsegroups(end).name);
      y=scanstruct.scan.loops(1).rng;
      xl=sprintf('XV from %s',scanstruct.scan.data.pulsegroups(end).name);
      yl=tostr(scanstruct.scan.loops(1).setchan);
      zl=tostr(scanstruct.scan.loops(2).getchan);
  else
      error(sprintf('Unrecognized scan format: %d dimensions, %d loops',ndims(d),length(scanstruct.scan.loops)));
  end
  
  %% Hack to cut out nan's.  
  
  if ~exist('svr','var') || isempty(svr)
      svr=spyview(d,x,y);      
  else
      svr.x=x;
      svr.y=y;
      svr.m=d;
  end
  
  ofs=dir(fname);  
  svr.reload_handler=@(svr) smspyview(fname,opts,svr);
  svr.xlabel=xl;
  svr.ylabel=yl;
  svr.zlabel=zl;
  svr.title=regexprep(regexprep(regexprep(fname,'^.*\\',''),'\.mat$',''),'^sm_',''); 
  
  try
    if ~isempty(which('pptprep'))  && exist(fname) && ~isempty(fname)
      pptprep(struct('file',fname,'figures',svr.f));
    end
  catch err
    fprintf('Warning: error setting up PPT output (%s)\n',getReport(err));
  end
  if nargout ~= 0
      svr_out=svr;
  end
end


% Apply a default.
function s=def(s,f,v)
if(~isfield(s,f))
    s=setfield(s,f,v);
end
end
function b=isopt(config,name)
b=~isempty(strfind(config.opts,name));
end

% Convert a cell array of channels to a nice string
function q=tostr(c)
  if ischar(c)
      q=c;
  elseif iscell(c)
      q=[sprintf('%s,',c{1:end-1}),c{end}];
  else
      q=string(c);
  end
end